package fccontext

import (
	"context"
	"gitee.com/ctyunfaas/cf-runtime-go-sdk/config"
	"gitee.com/ctyunfaas/cf-runtime-go-sdk/util"
)

type Credentials struct {
	AccessKeyId     string
	AccessKeySecret string
}

type Function struct {
	Name    string
	Handler string
	Memory  string
}

// 用户函数运行的上下文信息
type FcContext struct {
	RequestID   string
	Credentials Credentials
	Function    Function
	Region      string
	AccountId   string

	logger *FcLogger
}

func (f *FcContext) GetLogger() *FcLogger {
	return f.logger
}

// An unexported type to be used as the key for types in this package.
// This prevents collisions with keys defined in other packages.
type key struct{}

// The key for a LambdaContext in Contexts.
// Users of this package must use lambdacontext.NewContext and lambdacontext.FromContext
// instead of using this key directly.
var contextKey = &key{}

// 使用该方法创建 FcContext 对象
func NewContext(parent context.Context, lc *FcContext) context.Context {
	return context.WithValue(parent, contextKey, lc)
}

// FromContext returns the LambdaContext value stored in ctx, if any.
func FromContext(ctx context.Context) (*FcContext, bool) {
	lc, ok := ctx.Value(contextKey).(*FcContext)
	if !ok {
		return nil, false
	}

	li, ok := ctx.Value(config.LOG_TYPE).(*util.LoggerInterceptor)
	if !ok {
		return nil, false
	}

	lc.logger = NewFcLogger(lc.RequestID, li)
	return lc, ok
}
